﻿package net.nicoptere.media 
{
	import flash.display.Loader;
	import flash.media.Sound;
	import flash.net.URLRequest;
	import flash.utils.getDefinitionByName;
	import flash.events.Event;
	/**
	 * ...
	 * @author nicolas barradeau
	 */
	public class SoundItem 
	{
		/**
		 * 
		 * @param	id name of the item, to be used when calling SoundManager.play( id );
		 * @param	url an url from which to load a sound file
		 */
		
		protected var loader:Loader;
		public function SoundItem( id:String, url:String = null ) 
		{
			//storing the id
			_id = id;
			
			//will load from url
			if ( url != null )
			{	
				loader = new Loader();
				loader.load( new URLRequest( url ) );
				loader.contentLoaderInfo.addEventListener( Event.COMPLETE, onComplete );
			}
			else 
			{
				var c:Class = getDefinitionByName( 'id' ) as Class;
				sound = new c() as Sound;
				ready = true;
			}
			
			SoundManager.addItem( this );
			
		}
		protected function onComplete( e:Event ):void
		{
			loader.contentLoaderInfo.removeEventListener( Event.COMPLETE, onComplete );
			ready = true;
		}
		
		/**
		 * assigns the sound's id 
		 */
		private var _id:String;
		public function set id(value:String):void 
		{
			_id = value;
		}
		public function get id():String { return _id; }
		
		/**
		 * assigns the sound volume
		 */
		private var _volume:Number = 0.5;
		public function set volume(value:Number):void 
		{
			_volume = value;
		}
		public function get volume():Number { return _volume; }
		
		
		/**
		 * assigns the sound volume
		 */
		private var _sound:Sound;
		public function set sound(value:Sound):void 
		{
			_sound = value;
		}
		public function get sound():Number { return _sound; }
		
		
		/**
		 * tells SoundManager if this Item is ready to play
		 */
		private var _ready:Boolean = false;
		public function get ready():Boolean { return _ready; }
		
		
	}
	
}